/*    
 * Copyright (c) 2013 ISoftStone, Inc.
 * All right reserved.
 *
 * 文件名：      IChargingItemDao.java
 * 作者:     Jacky Wang
 * 创建日期： 2013-7-23 下午01:39:15
 * 版本：           
 *
 */
package com.isoftstone.industry.aaa.core.dao.assist;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.isoftstone.industry.persistance.bean.ChargingItem;
import com.isoftstone.modules.core.persistence.IGenericDao;

/**
 *
 * @author jacky
 */
public interface ChargingItemDao extends IGenericDao<ChargingItem, String>
{
	@Query("from ChargingItem where user.id=:userId and status=:status and (lastChargingTime is null or lastChargingTime<:now)")
	List<ChargingItem> findByUserAndStatusAndLastChargingTimeLessThan(@Param("userId")String userId, @Param("status")String status, @Param("now")Timestamp now);

	@Modifying
	@Query("update ChargingItem d set d.status = :status, d.statusTime= :modified where d.id = :id")
	void changeStatus(@Param("id") String id, @Param("status") String status, @Param("modified") Timestamp modified);
	
	@Modifying
	@Query("update ChargingItem d set d.lastChargingTime=:modifiedTime where d.id=:id")
	void changeLastChargingTime(@Param("id") String id, @Param("modifiedTime") Timestamp modifiedTime);
	
	@Query("select count(*) from ChargingItem where user.id=:userId and status=:status and (lastChargingTime is null or lastChargingTime<:now)")
	long countByUserAndStatusAndLastChargingTimeLessThan(@Param("userId")String userId, @Param("status")String status, @Param("now")Timestamp now);
	
	@Query("from ChargingItem where user.id=:userId and status=:status and (effDate<=:now and expDate>=:now)")
	List<ChargingItem> findEfficient(@Param("userId")String userId, @Param("status")String status, @Param("now")Timestamp now);
	
	@Query("from ChargingItem where user.id=:userId and status=:status and (effDate<=:end and expDate>=:start)")
	List<ChargingItem> findEfficientInBillingCycle(@Param("userId")String userId, @Param("status")String status, @Param("start")Date start, @Param("end") Date end);
	
	@Query("select count(*) from ChargingItem where user.id=:userId and status=:status and effDate<=:effdate and (lastChargingTime is null or lastChargingTime<:now)")
	long countByUserAndStatusAndLastChargingTimeAndEffDateLessThan(@Param("userId")String userId, @Param("status")String status, @Param("now")Timestamp now, @Param("effdate")Date effDate);
}
